package com.rede.didiok.admin.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rede.didiok.admin.entity.SystemConfigEntity;
import com.rede.didiok.admin.service.SystemConfigService;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.RedisConf;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.constant.global.ErrorCode;
import com.rede.didiok.common.dto.admin.SystemConfigDto;
import com.rede.didiok.common.enums.FilePriorityEnum;
import com.rede.didiok.common.enums.OpenStatusEnum;
import com.rede.didiok.common.enums.StatusEnum;
import com.rede.didiok.common.exception.exceptionType.QueryException;
import com.rede.didiok.common.feign.FileFeignClient;
import com.rede.didiok.common.utils.JsonUtils;
import com.rede.didiok.common.utils.RedisUtil;

/**
 * @Author: Rede
 * @DateTime: 2023-09-04 14:03
 * @Description: 文件相关操作的工具类
 */
@Slf4j
@Component
public class FileUtil {

    @Resource
    private SystemConfigService systemConfigService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    FileFeignClient fileFeignClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 格式化数据获取图片列表
     *
     * @param result
     * @return
     */
    public List<String> getPicture(String result) {
        String picturePriority = "";
        String localPictureBaseUrl = "";
        String qiNiuPictureBaseUrl = "";
        String minioPictureBaseUrl = "";
        String aliossPictureBaseUrl = "";
        // 从Redis中获取系统配置
        String systemConfigJson = redisUtil.get(RedisConf.SYSTEM_CONFIG);
        if (StringUtils.isEmpty(systemConfigJson)) {
            QueryWrapper<SystemConfigEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
            SystemConfigEntity systemConfig = systemConfigService.getOne(queryWrapper);
            if (systemConfig == null) {
                throw new QueryException(MessageConf.SYSTEM_CONFIG_IS_NOT_EXIST);
            } else {
                // 将系统配置存入Redis中【设置过期时间24小时】
                SystemConfigDto systemConfigDto = new SystemConfigDto();
                BeanUtils.copyProperties(systemConfig, systemConfigDto);
                redisUtil.setEx(RedisConf.SYSTEM_CONFIG, JsonUtils.objectToJson(systemConfigDto), 24, TimeUnit.HOURS);
            }
            picturePriority = systemConfig.getPicturePriority();
            localPictureBaseUrl = systemConfig.getLocalPictureBaseUrl();
            qiNiuPictureBaseUrl = systemConfig.getQiNiuPictureBaseUrl();
            minioPictureBaseUrl = systemConfig.getMinioPictureBaseUrl();
            aliossPictureBaseUrl = systemConfig.getAliossPictureBaseUrl();
        } else {
            SystemConfigDto systemConfig = JsonUtils.jsonToPojo(systemConfigJson, SystemConfigDto.class);
            if (systemConfig == null) {
                throw new QueryException(ErrorCode.QUERY_DEFAULT_ERROR, "系统配置转换错误，请检查系统配置，或者清空Redis后重试！");
            }
            picturePriority = systemConfig.getPicturePriority();
            localPictureBaseUrl = systemConfig.getLocalPictureBaseUrl();
            qiNiuPictureBaseUrl = systemConfig.getQiNiuPictureBaseUrl();
            minioPictureBaseUrl = systemConfig.getMinioPictureBaseUrl();
            aliossPictureBaseUrl = systemConfig.getAliossPictureBaseUrl();
        }

        List<String> picUrls = new ArrayList<>();
        try {
            Map<String, Object> picMap = (Map<String, Object>) JsonUtils.jsonToObject(result, Map.class);
            if (SysConf.SUCCESS.equals(picMap.get(SysConf.CODE))) {
                List<Map<String, Object>> picData = (List<Map<String, Object>>) picMap.get(SysConf.DATA);
                if (picData.size() > 0) {
                    for (int i = 0; i < picData.size(); i++) {
                        // 判断文件显示优先级【需要显示存储在哪里的图片】
                        if (FilePriorityEnum.QI_NIU.equals(picturePriority)) {
                            picUrls.add(qiNiuPictureBaseUrl + picData.get(i).get(SysConf.QI_NIU_URL));
                        } else if (FilePriorityEnum.MINIO.equals(picturePriority)) {
                            picUrls.add(minioPictureBaseUrl + picData.get(i).get(SysConf.MINIO_URL));
                        } else if (FilePriorityEnum.ALIOSS.equals(picturePriority)) {
                            picUrls.add(aliossPictureBaseUrl + picData.get(i).get(SysConf.ALIOSS_URL));
                        } else {
                            picUrls.add(localPictureBaseUrl + picData.get(i).get(SysConf.URL));
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("从Json中获取图片列表失败");
            log.error(e.getMessage());
            return picUrls;
        }
        return picUrls;
    }

    /**
     * 获取图片，返回Map
     *
     * @param result
     * @return
     */
    public List<Map<String, Object>> getPictureMap(String result) {

        String picturePriority = "";
        String localPictureBaseUrl = "";
        String qiNiuPictureBaseUrl = "";
        String minioPictureBaseUrl = "";
        String aliossPictureBaseUrl = "";
        // 从Redis中获取系统配置
        String systemConfigJson = redisUtil.get(RedisConf.SYSTEM_CONFIG);
        if (StringUtils.isEmpty(systemConfigJson)) {
            QueryWrapper<SystemConfigEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
            SystemConfigEntity systemConfig = systemConfigService.getOne(queryWrapper);
            if (systemConfig == null) {
                throw new QueryException(MessageConf.SYSTEM_CONFIG_IS_NOT_EXIST);
            } else {
                // 将系统配置存入Redis中【设置过期时间24小时】
                SystemConfigDto systemConfigDto = new SystemConfigDto();
                BeanUtils.copyProperties(systemConfig, systemConfigDto);
                redisUtil.setEx(RedisConf.SYSTEM_CONFIG, JsonUtils.objectToJson(systemConfigDto), 24, TimeUnit.HOURS);
            }
            picturePriority = systemConfig.getPicturePriority();
            localPictureBaseUrl = systemConfig.getLocalPictureBaseUrl();
            qiNiuPictureBaseUrl = systemConfig.getQiNiuPictureBaseUrl();
            minioPictureBaseUrl = systemConfig.getMinioPictureBaseUrl();
            aliossPictureBaseUrl = systemConfig.getAliossPictureBaseUrl();
        } else {
            SystemConfigDto systemConfigDto = JsonUtils.jsonToPojo(systemConfigJson, SystemConfigDto.class);
            picturePriority = systemConfigDto.getPicturePriority();
            localPictureBaseUrl = systemConfigDto.getLocalPictureBaseUrl();
            qiNiuPictureBaseUrl = systemConfigDto.getQiNiuPictureBaseUrl();
            minioPictureBaseUrl = systemConfigDto.getMinioPictureBaseUrl();
            aliossPictureBaseUrl = systemConfigDto.getAliossPictureBaseUrl();
        }

        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> picMap = (Map<String, Object>) JsonUtils.jsonToObject(result, Map.class);
        if (SysConf.SUCCESS.equals(picMap.get(SysConf.CODE))) {
            List<Map<String, Object>> picData = (List<Map<String, Object>>) picMap.get(SysConf.DATA);
            if (picData.size() > 0) {
                for (int i = 0; i < picData.size(); i++) {
                    Map<String, Object> map = new HashMap<>();
                    if (StringUtils.isEmpty(picData.get(i).get(SysConf.UID))) {
                        continue;
                    }
                    // 判断文件显示优先级【需要显示存储在哪里的图片】
                    if (FilePriorityEnum.QI_NIU.equals(picturePriority)) {
                        map.put(SysConf.URL, qiNiuPictureBaseUrl + picData.get(i).get(SysConf.QI_NIU_URL));
                    } else if (FilePriorityEnum.MINIO.equals(picturePriority)) {
                        map.put(SysConf.URL, minioPictureBaseUrl + picData.get(i).get(SysConf.MINIO_URL));
                    } else if (FilePriorityEnum.ALIOSS.equals(picturePriority)) {
                        map.put(SysConf.URL, aliossPictureBaseUrl + picData.get(i).get(SysConf.ALIOSS_URL));
                    } else {
                        map.put(SysConf.URL, localPictureBaseUrl + picData.get(i).get(SysConf.URL));
                    }

                    map.put(SysConf.UID, picData.get(i).get(SysConf.UID));
                    resultList.add(map);
                }
            }
        } else if (SysConf.ERROR.equals(picMap.get(SysConf.CODE))) {
            log.error("获取图片失败，图片服务出现异常：{}", picMap.get(SysConf.MESSAGE));
        } else {
            log.error("获取图片失败");
        }
        return resultList;
    }

    /**
     * 通过Token获取系统配置【返回Map类型】
     *
     * @return
     */
    public Map<String, String> getSystemConfigMap(String token) {
        // 判断该token的有效性【已在外层判断】
//        String adminJsonResult = stringRedisTemplate.opsForValue().get(RedisConf.LOGIN_TOKEN_KEY + Constants.SYMBOL_COLON + token);
//        if (StringUtils.isEmpty(adminJsonResult)) {
//            throw new QueryException(ErrorCode.INVALID_TOKEN, MessageConf.INVALID_TOKEN);
//        }
        // 从Redis中获取的SystemConf 或者 通过feign获取的
        SystemConfigEntity systemConfig = systemConfigService.getOneSystemConfig();

        // systemConfig 转成 map
        Map<String, String> resultMap = JsonUtils.object2Map(systemConfig);
        return resultMap;
    }

    /**
     * 获取系统配置，不论是Admin端还是Web端
     *
     * @return
     */
    public SystemConfigDto getSystemConfig() {
        SystemConfigDto systemConfig = new SystemConfigDto();
        ServletRequestAttributes attribute = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attribute.getRequest();
        // 后台携带的token
        Object token = request.getAttribute(SysConf.TOKEN);
        // 参数中携带的token
        String paramsToken = request.getParameter(SysConf.TOKEN);
        // 获取平台【web：门户，admin：管理端】
        String platform = request.getParameter(SysConf.PLATFORM);
        Map<String, String> systemConfigMap = new HashMap<>();
        // 判断是否是web端发送过来的请求【后端发送过来的token长度为32】
        if (SysConf.WEB.equals(platform)
                || (paramsToken != null && paramsToken.length() == Constants.THIRTY_TWO)
                || (token != null && token.toString().length() == Constants.THIRTY_TWO)) {
            // 如果是调用web端获取配置的接口
            if (token != null) {
                systemConfigMap = this.getSystemConfigByWebToken(token.toString());
                systemConfig.setToken(token.toString());
            } else {
                systemConfigMap = this.getSystemConfigByWebToken(paramsToken);
                systemConfig.setToken(paramsToken);
            }
            systemConfig.setPlatform(SysConf.WEB);
        } else {
            // 调用admin端获取配置接口
            if (token != null) {
                // 判断是否是后台过来的请求
                systemConfigMap = this.getSystemConfigMap(token.toString());
                systemConfig.setToken(token.toString());
            } else {
                // 判断是否是通过params参数传递过来的
                systemConfigMap = this.getSystemConfigMap(paramsToken);
                systemConfig.setToken(paramsToken);
            }
            systemConfig.setPlatform(SysConf.ADMIN);
        }

        if (systemConfigMap == null) {
            log.error(MessageConf.PLEASE_SET_QI_NIU);
            throw new QueryException(ErrorCode.PLEASE_SET_QI_NIU, MessageConf.PLEASE_SET_QI_NIU);
        }

        if (systemConfigMap == null) {
            throw new QueryException(ErrorCode.SYSTEM_CONFIG_NOT_EXIST, MessageConf.SYSTEM_CONFIG_NOT_EXIST);
        } else {
            String uploadQiNiu = systemConfigMap.get(SysConf.UPLOAD_QI_NIU);
            String uploadLocal = systemConfigMap.get(SysConf.UPLOAD_LOCAL);
            String localPictureBaseUrl = systemConfigMap.get(SysConf.LOCAL_PICTURE_BASE_URL);
            String qiNiuPictureBaseUrl = systemConfigMap.get(SysConf.QI_NIU_PICTURE_BASE_URL);
            String qiNiuAccessKey = systemConfigMap.get(SysConf.QI_NIU_ACCESS_KEY);
            String qiNiuSecretKey = systemConfigMap.get(SysConf.QI_NIU_SECRET_KEY);
            String qiNiuBucket = systemConfigMap.get(SysConf.QI_NIU_BUCKET);
            String qiNiuArea = systemConfigMap.get(SysConf.QI_NIU_AREA);

            String minioEndPoint = systemConfigMap.get(SysConf.MINIO_END_POINT);
            String minioAccessKey = systemConfigMap.get(SysConf.MINIO_ACCESS_KEY);
            String minioSecretKey = systemConfigMap.get(SysConf.MINIO_SECRET_KEY);
            String minioBucket = systemConfigMap.get(SysConf.MINIO_BUCKET);
            String uploadMinio = systemConfigMap.get(SysConf.UPLOAD_MINIO);
            String minioPictureBaseUrl = systemConfigMap.get(SysConf.MINIO_PICTURE_BASE_URL);

            String aliyunEndPoint = systemConfigMap.get(SysConf.ALI_YUN_ENDPOINT);
            String aliyunAccessKeySecret = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY_SECRET);
            String aliyunAccessKey = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY);
            String aliyunBucketName = systemConfigMap.get(SysConf.ALI_YUN_BUCKET_NAME);
            String uploadAlioss = systemConfigMap.get(SysConf.UPLOAD_ALIOSS);
            String aliossPictureBaseUrl = systemConfigMap.get(SysConf.ALI_OSS_PICTURE_BASE_URL);
            String aliyunCatalog= systemConfigMap.get(SysConf.ALI_YUN_CATALOG);
            // 判断七牛云参数是否存在异常
            if (OpenStatusEnum.OPEN.equals(uploadQiNiu) && (com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuPictureBaseUrl) || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuAccessKey)
                    || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuSecretKey) || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuBucket) || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuArea))) {
                throw new QueryException(ErrorCode.PLEASE_SET_QI_NIU, MessageConf.PLEASE_SET_QI_NIU);
            }
            // 判断本地服务参数是否存在异常
            if (OpenStatusEnum.OPEN.equals(uploadLocal) && com.rede.didiok.common.utils.StringUtils.isEmpty(localPictureBaseUrl)) {
                throw new QueryException(ErrorCode.PLEASE_SET_LOCAL, MessageConf.PLEASE_SET_LOCAL);
            }
            // 判断Minio服务是否存在异常
            if (OpenStatusEnum.OPEN.equals(uploadMinio) && (com.rede.didiok.common.utils.StringUtils.isEmpty(minioEndPoint) || com.rede.didiok.common.utils.StringUtils.isEmpty(minioPictureBaseUrl) || com.rede.didiok.common.utils.StringUtils.isEmpty(minioAccessKey)
                    || com.rede.didiok.common.utils.StringUtils.isEmpty(minioSecretKey) || com.rede.didiok.common.utils.StringUtils.isEmpty(minioBucket))) {
                throw new QueryException(ErrorCode.PLEASE_SET_MINIO, MessageConf.PLEASE_SET_MINIO);
            }

            //判断阿里OSS
            if (OpenStatusEnum.OPEN.equals(uploadAlioss) && (com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunEndPoint) || com.rede.didiok.common.utils.StringUtils.isEmpty(aliossPictureBaseUrl) || com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunAccessKey)
                    || com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunAccessKeySecret) || com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunBucketName))) {
                throw new QueryException(ErrorCode.PLEASE_SET_ALIOSS, MessageConf.PLEASE_SET_ALIOSS);
            }

            systemConfig.setQiNiuAccessKey(qiNiuAccessKey);
            systemConfig.setQiNiuSecretKey(qiNiuSecretKey);
            systemConfig.setQiNiuBucket(qiNiuBucket);
            systemConfig.setQiNiuArea(qiNiuArea);
            systemConfig.setUploadQiNiu(uploadQiNiu);
            systemConfig.setUploadLocal(uploadLocal);
            systemConfig.setPicturePriority(systemConfigMap.get(SysConf.PICTURE_PRIORITY));
            systemConfig.setContentPicturePriority(systemConfigMap.get(SysConf.CONTENT_PICTURE_PRIORITY));
            systemConfig.setLocalPictureBaseUrl(systemConfigMap.get(SysConf.LOCAL_PICTURE_BASE_URL));
            systemConfig.setQiNiuPictureBaseUrl(systemConfigMap.get(SysConf.QI_NIU_PICTURE_BASE_URL));

            systemConfig.setMinioEndPoint(minioEndPoint);
            systemConfig.setMinioAccessKey(minioAccessKey);
            systemConfig.setMinioSecretKey(minioSecretKey);
            systemConfig.setMinioBucket(minioBucket);
            systemConfig.setMinioPictureBaseUrl(minioPictureBaseUrl);
            systemConfig.setUploadMinio(uploadMinio);

            systemConfig.setAliyunEndpoint(aliyunEndPoint);
            systemConfig.setAliyunAccessKey(aliyunAccessKey);
            systemConfig.setAliyunAccessKeySecret(aliyunAccessKeySecret);
            systemConfig.setAliyunBucketName(aliyunBucketName);
            systemConfig.setAliossPictureBaseUrl(aliossPictureBaseUrl);
            systemConfig.setUploadAlioss(uploadAlioss);
            systemConfig.setAliyunCatalog(aliyunCatalog);

        }
        return systemConfig;
    }

    /**
     * 通过Token获取系统配置 【传入AdminToken】
     *
     * @param token
     * @return
     */
    public SystemConfigDto getSystemConfig(String token) {
        Map<String, String> systemConfigMap = this.getSystemConfigMap(token);
        SystemConfigDto systemConfig = new SystemConfigDto();
        if (systemConfigMap == null) {
            throw new QueryException(ErrorCode.SYSTEM_CONFIG_NOT_EXIST, MessageConf.SYSTEM_CONFIG_NOT_EXIST);
        } else {
            String uploadQiNiu = systemConfigMap.get(SysConf.UPLOAD_QI_NIU);
            String uploadLocal = systemConfigMap.get(SysConf.UPLOAD_LOCAL);
            String localPictureBaseUrl = systemConfigMap.get(SysConf.LOCAL_PICTURE_BASE_URL);
            String qiNiuPictureBaseUrl = systemConfigMap.get(SysConf.QI_NIU_PICTURE_BASE_URL);
            String qiNiuAccessKey = systemConfigMap.get(SysConf.QI_NIU_ACCESS_KEY);
            String qiNiuSecretKey = systemConfigMap.get(SysConf.QI_NIU_SECRET_KEY);
            String qiNiuBucket = systemConfigMap.get(SysConf.QI_NIU_BUCKET);
            String qiNiuArea = systemConfigMap.get(SysConf.QI_NIU_AREA);

            String minioEndPoint = systemConfigMap.get(SysConf.MINIO_END_POINT);
            String minioAccessKey = systemConfigMap.get(SysConf.MINIO_ACCESS_KEY);
            String minioSecretKey = systemConfigMap.get(SysConf.MINIO_SECRET_KEY);
            String minioBucket = systemConfigMap.get(SysConf.MINIO_BUCKET);
            String uploadMinio = systemConfigMap.get(SysConf.UPLOAD_MINIO);
            String minioPictureBaseUrl = systemConfigMap.get(SysConf.MINIO_PICTURE_BASE_URL);

            String aliyunEndPoint = systemConfigMap.get(SysConf.ALI_YUN_ENDPOINT);
            String aliyunAccessKeySecret = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY_SECRET);
            String aliyunAccessKey = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY);
            String aliyunBucketName = systemConfigMap.get(SysConf.ALI_YUN_BUCKET_NAME);
            String uploadAlioss = systemConfigMap.get(SysConf.UPLOAD_ALIOSS);
            String aliossPictureBaseUrl = systemConfigMap.get(SysConf.ALI_OSS_PICTURE_BASE_URL);
            String aliyunCatalog= systemConfigMap.get(SysConf.ALI_YUN_CATALOG);



            if (OpenStatusEnum.OPEN.equals(uploadQiNiu) && (com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuPictureBaseUrl) || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuAccessKey)
                    || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuSecretKey) || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuBucket) || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuArea))) {
                throw new QueryException(ErrorCode.PLEASE_SET_QI_NIU, MessageConf.PLEASE_SET_QI_NIU);
            }

            if (OpenStatusEnum.OPEN.equals(uploadLocal) && com.rede.didiok.common.utils.StringUtils.isEmpty(localPictureBaseUrl)) {
                throw new QueryException(ErrorCode.PLEASE_SET_LOCAL, MessageConf.PLEASE_SET_QI_NIU);
            }

            // 判断Minio服务是否存在异常
            if (OpenStatusEnum.OPEN.equals(uploadMinio) && (com.rede.didiok.common.utils.StringUtils.isEmpty(minioEndPoint) || com.rede.didiok.common.utils.StringUtils.isEmpty(minioPictureBaseUrl) || com.rede.didiok.common.utils.StringUtils.isEmpty(minioAccessKey)
                    || com.rede.didiok.common.utils.StringUtils.isEmpty(minioSecretKey) || com.rede.didiok.common.utils.StringUtils.isEmpty(minioBucket))) {
                throw new QueryException(ErrorCode.PLEASE_SET_MINIO, MessageConf.PLEASE_SET_MINIO);
            }
            //判断阿里OSS
            if (OpenStatusEnum.OPEN.equals(uploadAlioss) && (com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunEndPoint) || com.rede.didiok.common.utils.StringUtils.isEmpty(aliossPictureBaseUrl) || com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunAccessKey)
                    || com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunAccessKeySecret) || com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunBucketName))) {
                throw new QueryException(ErrorCode.PLEASE_SET_ALIOSS, MessageConf.PLEASE_SET_ALIOSS);
            }

            systemConfig.setQiNiuAccessKey(qiNiuAccessKey);
            systemConfig.setQiNiuSecretKey(qiNiuSecretKey);
            systemConfig.setQiNiuBucket(qiNiuBucket);
            systemConfig.setQiNiuArea(qiNiuArea);
            systemConfig.setUploadQiNiu(uploadQiNiu);
            systemConfig.setUploadLocal(uploadLocal);
            systemConfig.setPicturePriority(systemConfigMap.get(SysConf.PICTURE_PRIORITY));
            systemConfig.setContentPicturePriority(systemConfigMap.get(SysConf.CONTENT_PICTURE_PRIORITY));
            systemConfig.setLocalPictureBaseUrl(systemConfigMap.get(SysConf.LOCAL_PICTURE_BASE_URL));
            systemConfig.setQiNiuPictureBaseUrl(systemConfigMap.get(SysConf.QI_NIU_PICTURE_BASE_URL));

            systemConfig.setMinioEndPoint(minioEndPoint);
            systemConfig.setMinioAccessKey(minioAccessKey);
            systemConfig.setMinioSecretKey(minioSecretKey);
            systemConfig.setMinioBucket(minioBucket);
            systemConfig.setMinioPictureBaseUrl(minioPictureBaseUrl);
            systemConfig.setUploadMinio(uploadMinio);


            systemConfig.setAliyunEndpoint(aliyunEndPoint);
            systemConfig.setAliyunAccessKey(aliyunAccessKey);
            systemConfig.setAliyunAccessKeySecret(aliyunAccessKeySecret);
            systemConfig.setAliyunBucketName(aliyunBucketName);
            systemConfig.setAliossPictureBaseUrl(aliossPictureBaseUrl);
            systemConfig.setUploadAlioss(uploadAlioss);
            systemConfig.setAliyunCatalog(aliyunCatalog);


        }
        return systemConfig;
    }

    /**
     * 从Map中获取系统配置
     *
     * @param systemConfigMap
     * @return
     */
    public SystemConfigDto getSystemConfigByMap(Map<String, String> systemConfigMap) {
        SystemConfigDto systemConfig = new SystemConfigDto();
        if (systemConfigMap == null) {
            throw new QueryException(ErrorCode.SYSTEM_CONFIG_NOT_EXIST, MessageConf.SYSTEM_CONFIG_NOT_EXIST);
        } else {
            String uploadQiNiu = systemConfigMap.get(SysConf.UPLOAD_QI_NIU);
            String uploadLocal = systemConfigMap.get(SysConf.UPLOAD_LOCAL);
            String localPictureBaseUrl = systemConfigMap.get(SysConf.LOCAL_PICTURE_BASE_URL);
            String qiNiuPictureBaseUrl = systemConfigMap.get(SysConf.QI_NIU_PICTURE_BASE_URL);
            String qiNiuAccessKey = systemConfigMap.get(SysConf.QI_NIU_ACCESS_KEY);
            String qiNiuSecretKey = systemConfigMap.get(SysConf.QI_NIU_SECRET_KEY);
            String qiNiuBucket = systemConfigMap.get(SysConf.QI_NIU_BUCKET);
            String qiNiuArea = systemConfigMap.get(SysConf.QI_NIU_AREA);
            String picturePriority = systemConfigMap.get(SysConf.PICTURE_PRIORITY);
            String contentPicturePriority = systemConfigMap.get(SysConf.CONTENT_PICTURE_PRIORITY);

            String minioEndPoint = systemConfigMap.get(SysConf.MINIO_END_POINT);
            String minioAccessKey = systemConfigMap.get(SysConf.MINIO_ACCESS_KEY);
            String minioSecretKey = systemConfigMap.get(SysConf.MINIO_SECRET_KEY);
            String minioBucket = systemConfigMap.get(SysConf.MINIO_BUCKET);
            String uploadMinio = systemConfigMap.get(SysConf.UPLOAD_MINIO);
            String minioPictureBaseUrl = systemConfigMap.get(SysConf.MINIO_PICTURE_BASE_URL);

            String aliyunEndPoint = systemConfigMap.get(SysConf.ALI_YUN_ENDPOINT);
            String aliyunAccessKey = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY);
            String aliyunAccessKeySecret = systemConfigMap.get(SysConf.ALI_YUN_ACCESS_KEY_SECRET);
            String aliyunBucketName = systemConfigMap.get(SysConf.ALI_YUN_BUCKET_NAME);
            String uploadAlioss = systemConfigMap.get(SysConf.UPLOAD_ALIOSS);
            String aliossPictureBaseUrl = systemConfigMap.get(SysConf.ALI_OSS_PICTURE_BASE_URL);
            String aliyunCatalog= systemConfigMap.get(SysConf.ALI_YUN_CATALOG);


            if (OpenStatusEnum.OPEN.equals(uploadQiNiu) && (com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuPictureBaseUrl) || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuAccessKey)
                    || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuSecretKey) || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuBucket) || com.rede.didiok.common.utils.StringUtils.isEmpty(qiNiuArea))) {
                throw new QueryException(ErrorCode.PLEASE_SET_QI_NIU, MessageConf.PLEASE_SET_QI_NIU);
            }

            if (OpenStatusEnum.OPEN.equals(uploadLocal) && com.rede.didiok.common.utils.StringUtils.isEmpty(localPictureBaseUrl)) {
                throw new QueryException(ErrorCode.PLEASE_SET_LOCAL, MessageConf.PLEASE_SET_QI_NIU);
            }

            // 判断Minio服务是否存在异常
            if (OpenStatusEnum.OPEN.equals(uploadMinio) && (com.rede.didiok.common.utils.StringUtils.isEmpty(minioEndPoint) || com.rede.didiok.common.utils.StringUtils.isEmpty(minioPictureBaseUrl) || com.rede.didiok.common.utils.StringUtils.isEmpty(minioAccessKey)
                    || com.rede.didiok.common.utils.StringUtils.isEmpty(minioSecretKey) || com.rede.didiok.common.utils.StringUtils.isEmpty(minioBucket))) {
                throw new QueryException(ErrorCode.PLEASE_SET_MINIO, MessageConf.PLEASE_SET_MINIO);
            }

            //判断阿里OSS
            if (OpenStatusEnum.OPEN.equals(uploadAlioss) && (com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunEndPoint) || com.rede.didiok.common.utils.StringUtils.isEmpty(aliossPictureBaseUrl) || com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunAccessKey)
                    || com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunAccessKeySecret) || com.rede.didiok.common.utils.StringUtils.isEmpty(aliyunBucketName))) {
                throw new QueryException(ErrorCode.PLEASE_SET_ALIOSS, MessageConf.PLEASE_SET_ALIOSS);
            }

            systemConfig.setQiNiuAccessKey(qiNiuAccessKey);
            systemConfig.setQiNiuSecretKey(qiNiuSecretKey);
            systemConfig.setQiNiuBucket(qiNiuBucket);
            systemConfig.setQiNiuArea(qiNiuArea);
            systemConfig.setUploadQiNiu(uploadQiNiu);
            systemConfig.setUploadLocal(uploadLocal);
            systemConfig.setPicturePriority(picturePriority);
            systemConfig.setContentPicturePriority(contentPicturePriority);
            systemConfig.setContentPicturePriority(systemConfigMap.get(SysConf.CONTENT_PICTURE_PRIORITY));
            systemConfig.setLocalPictureBaseUrl(localPictureBaseUrl);
            systemConfig.setQiNiuPictureBaseUrl(qiNiuPictureBaseUrl);

            systemConfig.setMinioEndPoint(minioEndPoint);
            systemConfig.setMinioAccessKey(minioAccessKey);
            systemConfig.setMinioSecretKey(minioSecretKey);
            systemConfig.setMinioBucket(minioBucket);
            systemConfig.setMinioPictureBaseUrl(minioPictureBaseUrl);
            systemConfig.setUploadMinio(uploadMinio);


            systemConfig.setAliyunEndpoint(aliyunEndPoint);
            systemConfig.setAliyunAccessKey(aliyunAccessKey);
            systemConfig.setAliyunAccessKeySecret(aliyunAccessKeySecret);
            systemConfig.setAliyunBucketName(aliyunBucketName);
            systemConfig.setAliossPictureBaseUrl(aliossPictureBaseUrl);
            systemConfig.setUploadAlioss(uploadAlioss);
            systemConfig.setAliyunCatalog(aliyunCatalog);

        }
        return systemConfig;
    }


    /**
     * 通过Web端的token获取系统配置文件 【传入Admin端的token】
     *
     * @param token
     * @return
     */
    public Map<String, String> getSystemConfigByWebToken(String token) {
        // 判断该token的有效性
        String webUserJsonResult = stringRedisTemplate.opsForValue().get(RedisConf.USER_TOKEN + Constants.SYMBOL_COLON + token);
        if (com.rede.didiok.common.utils.StringUtils.isEmpty(webUserJsonResult)) {
            throw new QueryException(ErrorCode.INVALID_TOKEN, MessageConf.INVALID_TOKEN);
        }
        // 从Redis中获取的SystemConf 或者 通过feign获取的
        SystemConfigEntity systemConfig = systemConfigService.getOneSystemConfig();

        // systemConfig 转成 map
        Map<String, String> resultMap = JsonUtils.object2Map(systemConfig);
        return resultMap;
    }

    /**
     * 获取图片Map集合
     *
     * @return
     */
    public Map<String, String> fileUidToFileUrlMap(List<String> fileUidList) {
        Map<String, String> fileMap = new HashMap<>();
        final StringBuffer noCacheFileUids = new StringBuffer();
        for (String fileUid : fileUidList) {
            String fileUrl = redisUtil.get(RedisConf.FILE_UID_TO_URL + Constants.DELIMITER_COLON + fileUid);
            if (com.rede.didiok.common.utils.StringUtils.isNotEmpty(fileUrl)) {
                fileMap.put(fileUid, fileUrl);
            } else {
                noCacheFileUids.append(fileUid + SysConf.FILE_SEGMENTATION);
            }
        }

        // 解析map中的数据
        if (noCacheFileUids != null) {
            String pictureList = fileFeignClient.getPicture(noCacheFileUids.toString(), SysConf.FILE_SEGMENTATION);
            List<Map<String, Object>> picList = this.getPictureMap(pictureList);
            for (Map<String, Object> item : picList) {
                String fileUid = item.get(SQLConf.UID).toString();
                String fileUrl = item.get(SQLConf.URL).toString();
                fileMap.put(fileUid, fileUrl);
                // 将记录存储在redis中，设置过期时间10分钟
                if (com.rede.didiok.common.utils.StringUtils.isNotEmpty(fileUrl)) {
                    redisUtil.setEx(RedisConf.FILE_UID_TO_URL + Constants.DELIMITER_COLON + fileUid, fileUrl, 10, TimeUnit.MINUTES);
                }
            }
        }
        return fileMap;
    }

}
